package com.ruoyi.electronicFence.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.electronicFence.domain.ReceiveEFData;
import com.ruoyi.electronicFence.domain.TEfFenceArea;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.electronicFence.domain.TEfAlarm;
import com.ruoyi.electronicFence.service.ITEfAlarmService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 控制器报警信息
 *
 * @author sxygsj
 * @date 2024-05-20
 */
@RestController
@RequestMapping("/electronicFence/alarm")
public class TEfAlarmController extends BaseController
{
    @Autowired
    private ITEfAlarmService tEfAlarmService;

    /**
     * 查询电子围栏报警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:list')")
    @GetMapping("/details")
    public TableDataInfo list(TEfAlarm tEfAlarm)
    {
        startPage();
        List<TEfAlarm> list = tEfAlarmService.selectTEfAlarmList(tEfAlarm);
        return getDataTable(list);
    }



    /**
     * 查询电子围栏报警详情信息列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:list')")
    @GetMapping("/list")
    public TableDataInfo selectDetails(TEfAlarm tEfAlarm)
    {
        startPage();
        List<TEfAlarm> list = tEfAlarmService.selectTEfAlarmList2(tEfAlarm);
        return getDataTable(list);
    }

    /**
     * 查询电子围栏报警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:list')")
    @GetMapping("/details/list")
    public TableDataInfo selectDetailsList(TEfAlarm tEfAlarm)
    {
        //使用selectDetails 方法进行分页获取到 对应的List<Long> ids
        List<TEfAlarm> list2 = new ArrayList<>();
        TableDataInfo Details = selectDetails(tEfAlarm);
        List<TEfAlarm> list= (List<TEfAlarm>)Details.getRows();
        //获取数组总数
        int total = tEfAlarmService.selectDetailsListTotal(tEfAlarm);
        if(total>0){
            List<Long> ids = list.stream()
                    .map(TEfAlarm::getId)
                    .collect(Collectors.toList());
            tEfAlarm.setIds(ids);
            list2= tEfAlarmService.selectDetailsList(tEfAlarm);
        }


        TableDataInfo dataTable = getDataTable(list2);
        dataTable.setTotal(total);
        return dataTable;
    }


    /**
     * 导出电子围栏报警信息列表
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:export')")
    @Log(title = "电子围栏报警信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TEfAlarm tEfAlarm)
    {
        List<TEfAlarm> list = tEfAlarmService.selectTEfAlarmList(tEfAlarm);
        ExcelUtil<TEfAlarm> util = new ExcelUtil<TEfAlarm>(TEfAlarm.class);
        util.exportExcel(response, list, "电子围栏报警信息数据");
    }

    /**
     * 获取电子围栏报警信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tEfAlarmService.selectTEfAlarmById(id));
    }

    /**
     * 新增电子围栏报警信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:add')")
    @Log(title = "电子围栏报警信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TEfAlarm tEfAlarm)
    {
        return toAjax(tEfAlarmService.insertTEfAlarm(tEfAlarm));
    }

    /**
     * 修改电子围栏报警信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:edit')")
    @Log(title = "电子围栏报警信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TEfAlarm tEfAlarm)
    {
        return toAjax(tEfAlarmService.updateTEfAlarm(tEfAlarm));
    }

    /**
     * 删除电子围栏报警信息
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:remove')")
    @Log(title = "电子围栏报警信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tEfAlarmService.deleteTEfAlarmByIds(ids));
    }



    /**
     * 电子围栏报警信息入库 /receive-alarm-data
     * 模拟测试
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:remove')")
    @RequestMapping(value = "/receive-alarm-data/v2", method = {RequestMethod.GET, RequestMethod.POST})
    public AjaxResult InboundDataOftAlarm(@RequestBody(required = false) ReceiveEFData receiveEFData) {
//        Map<String, Object> maps = JSON.parseObject(map.get("data").toString());
        System.out.println(receiveEFData);
        int i = tEfAlarmService.InboundDataOftAlarmV2(receiveEFData);
        ReceiveEFData data1 = receiveEFData;
        int row =1;
        System.out.println(receiveEFData.getAlarmTime());

        if (row>0){
            return AjaxResult.success("入库成功");
        }else {
            return AjaxResult.error("入库失败");

        }
        // 入库数据

    }

    /**
     * 电子围栏报警信息入库 /receive-alarm-data
     * 模拟测试
     */
    //@PreAuthorize("@ss.hasPermi('electronicFence:alarm:remove')")
    @RequestMapping(value = "/receive-alarm-data", method = {RequestMethod.GET, RequestMethod.POST})
    public String InboundDataOftAlarmV2(@RequestBody(required = false) ReceiveEFData receiveEFData) {
//        Map<String, Object> maps = JSON.parseObject(map.get("data").toString());
        return "{\n" +
                "    \"Msg\": \"成功\",\n" +
                "    \"Result\": \"Succeed\",\n" +
                "    \"alarmTime\": \"2024-12-10 10:48:41\",\n" +
                "    \"alarmType\": [\n" +
                "        \"0\"\n" +
                "    ],\n" +
                "    \"apeIP\": \"\",\n" +
                "    \"apeName\": \"通道14\",\n" +
                "    \"controllerIP\": \"192.168.14.27\",\n" +
                "    \"controllerStatus\": \"2\",\n" +
                "    \"equipmentStatus\": \"-1\",\n" +
                "    \"personCardInfo\": {\n" +
                "        \"alarmList\": [\n" +
                "        ],\n" +
                "        \"shutdownList\": [\n" +
                "        ]\n" +
                "    },\n" +
                "    \"pictureUrl\": \"\"\n" +
                "}";

    }



}
